package com.weepal.pavement.mqtt.demo.analysis.resolver;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.weepal.pavement.mqtt.demo.util.test;
import com.weepal.pavement.mqtt.demo.vo.DeviceField;
import com.weepal.pavement.mqtt.demo.vo.MqttDevices;
import com.weepal.pavement.mqtt.demo.vo.Recursion;

import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * json数据类型解析器
 *
 * @author mawen
 */
public class JsonResolver {

    private String msg;
    private MqttDevices devices;

    private JsonResolver() {

    }

    public JsonResolver(MqttDevices devices, String msg) {
        this.msg = msg;
        this.devices = devices;
    }

    public ConcurrentHashMap<String, LinkedList<String>> resolve() throws JsonProcessingException {
        return resolveFields();
    }

    public ConcurrentHashMap<String, LinkedList<String>> resolveFields() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        //解析json,可能有多层嵌套
        JsonNode jsonNode = mapper.readTree(msg);
        //获得设备配置的所有字段
        ConcurrentHashMap<String, DeviceField> fields = devices.getFields();
        ConcurrentHashMap<String, LinkedList<String>> result = new ConcurrentHashMap<>();

        for (Map.Entry<String, DeviceField> entry : fields.entrySet()) {
            LinkedList<String> datas = resolveDataByField(jsonNode, entry);
            result.put(entry.getKey(), datas);
        }
        return result;
    }

    public LinkedList<String> resolveDataByField(JsonNode jsonNode, Map.Entry<String, DeviceField> entry) {
        String key = entry.getKey();//外层的key，即DeviceField的jsonKey，entery是DeviceField
        JsonNode v = jsonNode.get(key);
        LinkedList<String> datas = null;
        if (null != v) {
            DeviceField field = entry.getValue();
            datas = new LinkedList<>();
            String vStr = v.toString();
            if (field.getReadFields() == null) {
                if (field.getChangeType() != 0) {
                    vStr = test.hex2dec(vStr, field.getChangeType());
                }
                datas.addLast(replace(vStr));
                return datas;
            }
            String[] f = field.getReadFields().split(",");
            for (String ff : f) {
                if (ff.indexOf(".") != -1) {//如果是嵌套字段"a":{"b":{"c":"123"}}
                    String[] keys = ff.split(".");
                    Recursion recursion = new Recursion();
                    getDeepVal(keys, v, 0, recursion);
                    datas.addLast(replace(recursion.getData()));
                    continue;
                }
                JsonNode result = v.get(ff);
                //防止解析出来的带上双引号了
                datas.addLast(replace(result.toString()));
            }
        }
        return datas;
    }

    //递归查询
    public void getDeepVal(String[] keys, JsonNode v, int index, Recursion vv) {
        if (index < keys.length - 1) {
            getDeepVal(keys, v.get(keys[index]), ++index, vv);
        } else {
            vv.setData(v.get(keys[index]).toString());
        }
    }

    public String replace(String v) {
        return v.replaceAll("\"", "");
    }
}
